package com.bosssoft.setp.handler;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.AttachmentProxy;
import com.qcloud.cos.region.Region;
import java.io.InputStream;

/**
 * 腾讯云服务上传工具类
 *
 * @author clt
 */
@Service
public class TencentOSSProxy implements AttachmentProxy {
    @Value("${tencent.oss.folderPrefix}")
    private String prePath;

    @Value("${tencent.oss.bucketName}")
    private String bucketName;

    @Value("${tencent.oss.accessKey}")
    private String SECRET_ID;

    @Value("${tencent.oss.secretKey}")
    private String SECRET_KEY;

    @Value("${tencent.oss.regionName}")
    private String regionName;

    @Value("${tencent.oss.path}")
    private String url;
    @Override
    public String upLoad(InputStream inputStream, String path) {
        COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        COSClient cosClient = new COSClient(cred, clientConfig);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        String key = prePath+path;
        PutObjectResult result = cosClient.putObject(bucketName, key, inputStream, objectMetadata);
        if (result != null) {
            return key;
        }
        return null;
    }

    @Override
    public String fileDomain() {
        return url;
    }

    @Override
    public boolean isLocalSave() {
        return false;
    }
}